package com.hexu.event.view;

import com.hexu.event.entity.Attractions;
import com.hexu.event.util.StageUtil;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.hexu.event.util.ServiceUtil.attractionsService;
import static com.hexu.event.view.AdministratorView.view;

public class VisitorsView {

    //取出景点的所有信息
    private static List<Attractions> listAttractionsAllInformation = attractionsService.getAttractionsInformation();

    //获取景点数量
    private static int attractionsCount = attractionsService.attractionsNumber();

    //获取当前日期
    static Calendar now = Calendar.getInstance();
    static int year = now.get(Calendar.YEAR);
    static int month = now.get(Calendar.MONTH) + 1;
    static int today = now.get(Calendar.DAY_OF_MONTH);
    //存选中的时间
    static String yeardesc = String.valueOf(year);
    static String monthdesc = String.valueOf(month);
    static String daydesc;
    static final int[] day = {0};


    public void visitorsView(Stage primaryStage)throws Exception{

        //设置按钮
        Button searchAttractions = new Button("查看景点");
        Button loginIN = new Button("登录");
        Button register = new Button("注册");

        //设置起始点的X、Y轴坐标
        searchAttractions.setLayoutX(50);
        searchAttractions.setLayoutY(200);
        loginIN.setLayoutX(200);
        loginIN.setLayoutY(200);
        register.setLayoutX(350);
        register.setLayoutY(200);
        //设置button的宽度 高度
        searchAttractions.setPrefWidth(100);
        loginIN.setPrefWidth(100);
        register.setPrefWidth(100);
        searchAttractions.setPrefHeight(50);
        loginIN.setPrefHeight(50);
        register.setPrefHeight(50);

        //创建Group容器组
        Group group = new Group();

        //添加按钮
        group.getChildren().addAll(searchAttractions,loginIN,register);
        //调用工具类显示界面
        StageUtil.showStage(primaryStage,new Scene(group),500,500,"游客界面",false);

        //设置 查看景点 单击事件
        searchAttractions.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //表示是游客
                //更新信息
                listAttractionsAllInformation = attractionsService.getAttractionsInformation();
                attractionsCount = attractionsService.attractionsNumber();
                view(primaryStage,0,null,attractionsCount,listAttractionsAllInformation);

            }
        });

        //设置 登录 单击事件
        loginIN.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //关闭这个界面，打开登录界面
                primaryStage.close();
                LoginInView loginInView = new LoginInView();
                try {
                    loginInView.start(primaryStage);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        //设置 注册 单击事件
        register.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //关闭这个界面，打开注册界面
                primaryStage.close();
                RegisterView registerView = new RegisterView();
                try {
                    registerView.registerView(primaryStage);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    //显示当前时间
    public static String dateDisplay() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    // 判断一个月有多少天
    private static int dayNumber(String stringYear, String stringMonth){
        int day = 0;
        int year = Integer.valueOf(stringYear);
        int month = Integer.valueOf(stringMonth);
        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
            day = 31;
        }
        if(month==4 || month==6 || month==9 || month==11){
            day = 30;
        }
        if(month == 2){
            if(((year%4==0)&&(year%100!=0))||(year%400==0)){
                day = 29;
            }else{
                day = 28;
            }
        }
        return day;
    }

    //  定义日期字符串
    public static String strDate(){
        // 定义日期字符串
        String strDate = yeardesc+"-";
        if (Integer.valueOf(monthdesc) < 10) {
            strDate = strDate+"0";
        }
        strDate = strDate+monthdesc+'-';
        if (Integer.valueOf(daydesc) < 10) {
            strDate = strDate+"0";
        }
        strDate = strDate+daydesc;

        return strDate;
    }
    // 根据日期取得星期几
    private static String getWeek(Date date) {
        String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }



    public static ComboBox<String> createYear(){
        //开始默认获取年份为year
        yeardesc = String.valueOf(year);
        //存储要选择的年份
        List<String> snackList = Arrays.asList(String.valueOf(year),String.valueOf(year+1));
        // 把清单对象转换为JavaFX控件能够识别的数据对象
        ObservableList<String> obList = FXCollections.observableArrayList(snackList);
        // 依据指定数据创建下拉框
        ComboBox<String> comboBox = new ComboBox<String>(obList);
        // 设置下拉框默认选中第1项
        comboBox.getSelectionModel().select(0);
        // 设置下拉框的选择监听器
        comboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> arg0, String old_str, String new_str) {
                // getSelectedIndex方法可获得选中项的序号，getSelectedItem方法可获得选中项的对象
                yeardesc = comboBox.getSelectionModel().getSelectedItem();

            }
        });

        return comboBox;
    }


    public static ComboBox[] createMonthAndDay(){

        //开始默认获取月份为month
        monthdesc = String.valueOf(month);
        List<String> snackList2 = Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12");
        // 把清单对象转换为JavaFX控件能够识别的数据对象
        ObservableList<String> obList2 = FXCollections.observableArrayList(snackList2);
        // 依据指定数据创建下拉框
        ComboBox<String> comboBox2 = new ComboBox<String>(obList2);
        // 设置下拉框默认选择现在月份
        comboBox2.getSelectionModel().select( monthdesc);

        //开始默认获取日为today
        daydesc = String.valueOf(today);
        // 把清单对象转换为JavaFX控件能够识别的数据对象
        ObservableList<String> obList3 = FXCollections.observableArrayList(getSnackList3());
        // 依据指定数据创建下拉框
        ComboBox<String> comboBox3 = new ComboBox<String>(obList3);
        // 设置下拉框默认选中今天
        comboBox3.getSelectionModel().select(daydesc);

        ComboBox[] arr = new ComboBox[]{comboBox2,comboBox3};

        // 设置月份下拉框的选择监听器
        comboBox2.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> arg0, String old_str, String new_str) {
                monthdesc = comboBox2.getSelectionModel().getSelectedItem();

                // 把清单对象转换为JavaFX控件能够识别的数据对象
                ObservableList<String> obList3 = FXCollections.observableArrayList(getSnackList3());
                comboBox3.setItems(obList3);
                // 设置下拉框默认选中1号
                comboBox3.getSelectionModel().select(0);
                daydesc = "1";
            }
        });

        // 设置日期下拉框的选择监听器
        comboBox3.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> arg0, String old_str, String new_str) {
                daydesc = comboBox3.getSelectionModel().getSelectedItem();
            }
        });

        return arr;
    }

    public static List<String> getSnackList3(){
        //可能改变的每月天数
        day[0] = dayNumber(yeardesc,monthdesc);
        List<String> snackList3 = new ArrayList<>();
        for(int i = 1;i <= day[0];i++){
            snackList3.add(String.valueOf(i));
        }
        return snackList3;
    }

    public static Button buttonView(int visitorsOrUsersOrAdministrator,String phoneNumber,Stage stage){
        Button button = new Button("确定查询");
        //设置 确定查询 单击事件
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse(strDate());// 将字符串转换为日期
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //获得日期是周几
                String week = getWeek(date);
                //更新信息
                listAttractionsAllInformation = attractionsService.getAttractionsInformation();
                attractionsCount = attractionsService.attractionsNumber();

                //把这天开放的景点名字和数量取出
                List<Attractions> openAttractions = new ArrayList<>();
                int openCount = 0;
                for(int i = 0;i< attractionsCount ;i++){
                    if(  listAttractionsAllInformation.get(i).getOpenTime().contains(week)){
                        openAttractions.add(  listAttractionsAllInformation.get(i));
                        openCount++;
                    }
                }
                stage.close();
                //展示开放的景点
                view(new Stage(),visitorsOrUsersOrAdministrator,phoneNumber,openCount,openAttractions);

            }
        });

        return button;
    }

}